﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SkypeBot.Core
{
    public class AccordionDetector
    {
        private static List<AccordionModel> _accordionList;
        public static List<AccordionModel> AccordionList
        {
            get
            {
                if (_accordionList == null)
                    _accordionList = new List<AccordionModel>();
                return _accordionList;
            }
        }

        public AccordionDetector()
        {
                
        }

        public static void CheckMessageOnAccordion(Message objMessage)
        {
            Regex regLink = new Regex(@"^.*(?<link>\bhttp(s?)://.?[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]\b).*$");
            Match objMatch = regLink.Match(objMessage.MessageContent);
            if (objMatch.Success)
            {
                if (AccordionList.Count > 0)
                {
                    int count = AccordionList.Count;
                    bool IsInCollection = false;
                    for (int i = 0; i < count; i++)
                    {
                        if (AccordionList[i].Link == objMatch.Groups["link"].Value)
                        {
                            IsInCollection = true;
                            string strResponce = string.Format("Обнаружен боян!!!{0} кидал {1} в {2}", AccordionList[i].UserName, AccordionList[i].Link, AccordionList[i].FirstTimeSent);
                            Core.Instance.SendToRecievers(new Responce(strResponce));
                        }
                    }

                    if (!IsInCollection) { AccordionList.Add(new AccordionModel(objMessage.SenderName, objMatch.Groups["link"].Value, DateTime.Now));}
                }
                else { AccordionList.Add(new AccordionModel(objMessage.SenderName, objMatch.Groups["link"].Value, DateTime.Now)); }
            }
        }
    }

    public class AccordionModel
    {
        public AccordionModel(string strUserName, string strLink, DateTime objDate)
        {
            UserName = strUserName;
            Link = strLink;
            FirstTimeSent = objDate;
        }

        public string UserName { get; set; }
        public string Link { get; set; }
        public DateTime FirstTimeSent { get; set; }
    }
}
